import json
import re
import time
from copy import copy
from datetime import date, datetime
import openpyxl

import requests
import xlrd
import xlwt

from openpyxl import load_workbook, Workbook




head={
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.30",
        "cookie":"FZ_STROAGE.ketangpai.com=eyJTRUVTSU9OSUQiOiI2YTIxZGViMGQ1MThlZGE2IiwiU0VFU0lPTkRBVEUiOjE2NDYyNzkwODMxODh9; ARK_ID=undefined; ketangpai_home_slb=69fe0169927fb731c0cb96fa3ba1db0c; PHPSESSID=dg6l1psl62smhv8tir1kp57t16; ketangpai_home_remember=think%3A%7B%22username%22%3A%22MDAwMDAwMDAwMLOGy5iH379phd6Yl7LPgZ4%22%2C%22expire%22%3A%22MDAwMDAwMDAwMLOGvZSHz7dphbic3rHMdZ4%22%2C%22token%22%3A%22MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p-q6iZu2yrn4uNhJ3KedDYk7ivboS4jpSyz4mShLeZ3n6gYW0%22%2C%22sign%22%3A%22a1c07c6bf2acb5cf106694db24f648e5%22%7D"
        #"cookie":"SESSION=5459a040-3c90-42d5-9fe2-cfa4765cab3f; __pstsid__=b8f78877a874fe0eca6f273364469af5"

    }
def getname(i):
    url=f'https://v4.ketangpai.com/StudentsApi/lists?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&txt=&page={i}'
    #url="http://jwxt.jmu.edu.cn/student/for-std/course-table/semester/181/print-data/0/4759"

    response=requests.get(url=url,headers=head)
    str= json.dumps(response.json(), ensure_ascii=False)
    #print(str)
    #print(type(str))
    myname=re.compile(r'.*?"username":\s"(?P<name>.*?)",.*?',re.S)
    result=re.finditer(myname,str)
    for it in result:
        print(it.group("name"))
def getid(i):
    url=f'https://v4.ketangpai.com/StudentsApi/lists?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&txt=&page={i}'
    #url="http://jwxt.jmu.edu.cn/student/for-std/course-table/semester/181/print-data/0/4759"

    response=requests.get(url=url,headers=head)
    str= json.dumps(response.json(), ensure_ascii=False)
    #print(str)
    #print(type(str))
    myid=re.compile(r'.*?"id":\s"(?P<id>.*?)",.*?',re.S)
    result=re.finditer(myid,str)
    for it in result:
        print(it.group("id"))

#生成学生列表包含学号姓名班级
def getlis(lis):
    f1=open("dict.txt","r",encoding="utf-8")
    lows=f1.readlines()
    for i in lows:
        z=i.strip("\n").split("\t")
        lis.append(z)
    #print(lis)
    f1.close()
    return lis

def getkaoqing(url,a):
    #url="https://openapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2pZiGubtohaVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    #url="https://openapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2qdyG36dshNtyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    #url="https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2qd6Hz7-yhqVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    #url="https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2qd6Hz7-yhqVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    guize=re.compile(r'username":\s"(?P<name>.*?)",.*?,\s"state":\s"(?P<state>\d)"')
    result = re.finditer(guize, str)
    #print(str)#打印html页面
    for it in result:
         name=it.group("name")
         state=it.group("state")
         #print(name,state)     #打印所有考勤名单
         if state=="1":#如果state==1则未考勤
            print(name)
            lis=[]
            getlis(lis)
            for i in lis:
                if i[1] == name:
                    sco=i[0]
                    banji=i[2]
                else:
                    pass
            ws.append([banji, sco, name, a, ' ', '1', ' '])

def getkaoqingid():
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    print(str)
    idguize=re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"',re.S)
    result=re.finditer(idguize,str)
    for it in result:
        print(it.group("id"))
        print(it.group("title"))
    print(str)

def getshujukukaoqin():
    print("以下是数据库未考勤名单！！！")
    url = "https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    #print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)".*?createtime": "(?P<create_time>.*?)",', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        kaoqintime=(int(it.group("create_time")))
        timeArray = time.localtime(kaoqintime)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)#这是考勤时间
        #print(otherStyleTime)
        dt1 = date(int(otherStyleTime[0:4]), int(otherStyleTime[5:7]), int(otherStyleTime[8:10]))
        dt1.strftime("%V")
        week=int(dt1.strftime("%V"))#表示周数
        #print(week)
        #print(type(week))
        nowweek=datetime.now().isocalendar()[1]
        #nowweek=9
        #print(type(nowweek))
        #print(nowweek)
        if(week==nowweek):
            #print(it.group("id"))
            print(it.group("title"))  # 打印时间
            url=f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
            getkaoqing(url,it.group("title")[5:]+"数据库")
        #print(str)#打印html页面

def getcaozuoxitongkaoqin():
    print("以下是操作系统未考勤名单")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiGz6tohctyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)".*?createtime": "(?P<create_time>.*?)",', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        kaoqintime = (int(it.group("create_time")))
        timeArray = time.localtime(kaoqintime)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)  # 这是考勤时间
        # print(otherStyleTime)
        dt1 = date(int(otherStyleTime[0:4]), int(otherStyleTime[5:7]), int(otherStyleTime[8:10]))
        dt1.strftime("%V")
        week = int(dt1.strftime("%V"))  # 表示周数
        # print(week)
        # print(type(week))
        nowweek = datetime.now().isocalendar()[1]
        #nowweek = 9
        # print(type(nowweek))
        # print(nowweek)
        if (week == nowweek):
            #print(it.group("id"))
            print(it.group("title"))  # 打印时间
            url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
            getkaoqing(url,it.group("title")[5:]+"操作系统")
        # print(str)#打印html页面

def getmakesikaoqin():
    print("以下是马克思原理未考勤名单")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiG381shMtyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)".*?createtime": "(?P<create_time>.*?)",', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        kaoqintime = (int(it.group("create_time")))
        timeArray = time.localtime(kaoqintime)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)  # 这是考勤时间
        # print(otherStyleTime)
        dt1 = date(int(otherStyleTime[0:4]), int(otherStyleTime[5:7]), int(otherStyleTime[8:10]))
        dt1.strftime("%V")
        week = int(dt1.strftime("%V"))  # 表示周数
        # print(week)
        # print(type(week))
        nowweek = datetime.now().isocalendar()[1]
        #nowweek = 9
        # print(type(nowweek))
        # print(nowweek)
        if (week == nowweek):
            #print(it.group("id"))
            print(it.group("title"))  # 打印时间
            url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
            getkaoqing(url,it.group("title")[5:]+"马克思原理")
        # print(str)#打印html页面

def getgailulun():
    print("以下是概率论未考勤名单")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiGz7tthrVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)".*?createtime": "(?P<create_time>.*?)",', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        kaoqintime = (int(it.group("create_time")))
        timeArray = time.localtime(kaoqintime)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)  # 这是考勤时间
        # print(otherStyleTime)
        dt1 = date(int(otherStyleTime[0:4]), int(otherStyleTime[5:7]), int(otherStyleTime[8:10]))
        dt1.strftime("%V")
        week = int(dt1.strftime("%V"))  # 表示周数
        # print(week)
        # print(type(week))
        nowweek = datetime.now().isocalendar()[1]
        #nowweek = 9
        # print(type(nowweek))
        # print(nowweek)
        if (week == nowweek):
            #print(it.group("id"))
            print(it.group("title"))  # 打印时间
            url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
            getkaoqing(url,it.group("title")[5:]+"概率论")
        # print(str)#打印html页面

def getjisuanjiwangluo():
    print("以下是计算机网络未考勤名单")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiGz6tohKVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeV3n6GdmLIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)".*?createtime": "(?P<create_time>.*?)",', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        kaoqintime = (int(it.group("create_time")))
        timeArray = time.localtime(kaoqintime)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)  # 这是考勤时间
        # print(otherStyleTime)
        dt1 = date(int(otherStyleTime[0:4]), int(otherStyleTime[5:7]), int(otherStyleTime[8:10]))
        dt1.strftime("%V")
        week = int(dt1.strftime("%V"))  # 表示周数
        # print(week)
        # print(type(week))
        nowweek = datetime.now().isocalendar()[1]
        #nowweek = 9
        # print(type(nowweek))
        # print(nowweek)
        if (week == nowweek):
            #print(it.group("id"))
            print(it.group("title"))  # 打印时间
            url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
            getkaoqing(url,it.group("title")[5:]+"计算机网络")
        # print(str)#打印html页面

if __name__ == '__main__':
    # for i in range(1,8):
    #     getname(i)
    #     getid(i)
    #getkaoqing()
    #getkaoqingid()
    wb = Workbook()
    ws = wb.active

    ws.append([f'计算机工程学院计算智能专业22学年第二学期第{datetime.now().isocalendar()[1]}周缺勤情况确认'])
    ws.append(['班级', '学号', '姓名', '具体时间', '迟到', '缺勤', '晚归', '确定签名', '备注'])
    getshujukukaoqin()#数据库原理考勤
    getcaozuoxitongkaoqin()#操作系统考勤
    getmakesikaoqin()#马克思原理考勤
    getgailulun()#概率论与数理统计考勤
    getjisuanjiwangluo()#计算机网络考勤

    wb.save('kaoqin.xlsx')